#include<stdio.h>
#include<stdlib.h>
typedef struct Node* ptrNode;
typedef struct Node
{
	int zhishu;
	int xishu;
	ptrNode next;
}Node;
typedef ptrNode List;
List Read()
{
	int len=0;
	scanf("%d",&len);
	List L=(List)malloc(sizeof(Node));
	L->next=NULL;
	List p=L;
	int n=0,m=0;
	while(len>0)
	{
		scanf("%d",&n);
		scanf("%d",&m);
		List node=(List)malloc(sizeof(Node));
		node->next=NULL;
		node->xishu=n;
		node->zhishu=m;
		p->next=node;
		p=p->next;
		len--;
	}
	return L;
}
List jia(List L1,List L2)
{
	List L=(List)malloc(sizeof(Node));
	L->next=NULL;
	List p=L;
	List p1=L1->next;
	List p2=L2->next;
	while(p1!=NULL && p2!=NULL)
	{
		List node=(List)malloc(sizeof(Node));
		node->next=NULL;
		if(p1->zhishu<p2->zhishu)
		{
			node->xishu=p2->xishu;
			node->zhishu=p2->zhishu;
			p->next=node;
			p=p->next;
			p2=p2->next;
		}
		else if(p1->zhishu>p2->zhishu)
		{
			node->xishu=p1->xishu;
			node->zhishu=p1->zhishu;
			p->next=node;
			p=p->next;
			p1=p1->next;
		}
		else
		{
			node->xishu=p1->xishu+p2->xishu;
			node->zhishu=p1->zhishu;
			if(node->xishu==0)
			{
				p1=p1->next;
				p2=p2->next;
				continue;
			}
			p->next=node;
			p=p->next;
			p1=p1->next;
			p2=p2->next;
		}
	}
	if(p1)
		p->next=p1;
	if(p2)
		p->next=p2;
	return L;
}
List cheng(List L1,List L2)
{
	List L=(List)malloc(sizeof(Node));
	L->next=NULL;
	List p=L;
	List p1=L1->next;
	List p2=L2->next;
	int n=1;
	while(p1!=NULL)
	{
		List S=(List)malloc(sizeof(Node));
		S->next=NULL;
		if(n>=2)
		{
			p=S;
		}
		while(p2!=NULL)
		{
			List node=(List)malloc(sizeof(Node));
			node->next=NULL;
			node->xishu=p1->xishu*p2->xishu;
			node->zhishu=p1->zhishu+p2->zhishu;
			p->next=node;
			p=p->next;
			p2=p2->next;
		}
		if(n>=2)
		{
			L=jia(L,S);
		}
		n+=1;
		p1=p1->next;
		p2=L2->next;
	}
	return L;
}
void Print(List L)
{
	L=L->next;
	if(L==NULL)
		printf("0 0");
	while(L!=NULL)
	{
		if(L->next==NULL)
		{
			printf("%d %d",L->xishu,L->zhishu);
			break;
		}
		printf("%d %d ",L->xishu,L->zhishu);
		L=L->next;
	}
	printf("\n");
}
int main()
{
	List L=(List)malloc(sizeof(Node));
	L->next=NULL;
	List S=(List)malloc(sizeof(Node));
	S->next=NULL;
	L=Read();
	S=Read();
	List p=jia(L,S);
	List q=cheng(L,S);
	Print(q);
	Print(p);
	return 0;
}
